//导入数据库API
const db = require('../../config/db');
//操作数据库的业务
module.exports = {
    // //查询用户列表
    hotList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_hot`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    allList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_all`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    topList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_top`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    drinkList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_drink`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    foodList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_food`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    goodsList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_goods`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    shopList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_shop`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    ContentList(con_id) {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_content where con_id = "${con_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    // 修改价格
    modify(con_id, con_total) {
        return new Promise((resolve, reject) => {
            var sql = `update db_coffee_content set con_total= "${con_total}" where con_id= "${con_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改用户资料失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "修改用户资料成功。",
                        result: con_total
                    })
                }
            })
        })
    },
    // 增加数据给购物车列表
    add(car_id, car_name, car_img, car_cup, car_temp, car_sugar, car_total) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_car(car_id, car_name,car_img, car_cup, car_temp, car_sugar, car_total) values ("${car_id}","${car_name}","${car_img}","${car_cup}","${car_temp}","${car_sugar}","${car_total}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加用户失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加用户成功。",
                        car_id,
                        car_name
                    })
                }
            })
        })
    },
    // 查询购物车全部数据
    carList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_car`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    // 删除购物车列表数据
    remove(car_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_coffee_car where car_id = "${car_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除购物车列表失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "购物车列表"
                    })
                }
            })
        })
    },
    // 删除购物车全部数据
    removeCar() {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_coffee_car `
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除购物车列表失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "购物车列表"
                    })
                }
            })
        })
    },
    // 搜索内容数据
    countList(x) {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_content where con_name like "%${x}%"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,

                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows,
                        msg: '没有该产品'
                    })
                }
            })
        })
    },
    // 添加订单
    addOrder(ord_id, ord_name, ord_address, ord_count, ord_total, ord_time) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_order(ord_id, ord_name, ord_address, ord_count, ord_total,ord_time) values ("${ord_id}","${ord_name}","${ord_address}","${ord_count}","${ord_total}","${ord_time}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加订单失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加订单成功。",
                        ord_id,
                        ord_name
                    })
                }
            })
        })
    },
    //查询订单
    orderList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_order`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    //删除订单
    removeOrder(ord_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_coffee_order where ord_id = "${ord_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除订单失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "删除订单成功。"
                    })
                }
            })
        })
    },
    //添加已完成订单
    addFish(finish_id, finish_name, finish_address, finish_count, finish_total, finish_time) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_finish(finish_id, finish_name, finish_address, finish_count, finish_total, finish_time) values ("${finish_id}","${finish_name}","${finish_address}","${finish_count}","${finish_total}","${finish_time}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加订单失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加订单成功。",
                        finish_id,
                        finish_name
                    })
                }
            })
        })
    },
    //查询已完成订单
    finishList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_finish`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    //添加未完成订单
    addUndone(undone_id, undone_name, undone_address, undone_count, undone_total, undone_time) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_undone(undone_id, undone_name, undone_address, undone_count, undone_total, undone_time) values ("${undone_id}","${undone_name}","${undone_address}","${undone_count}","${undone_total}","${undone_time}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加订单失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加订单成功。",
                        undone_id,
                        undone_name
                    })
                }
            })
        })
    },
    //删除未完成订单
    removeUndone(undone_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from db_coffee_undone where undone_id = "${undone_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除订单失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "删除订单成功。"
                    })
                }
            })
        })
    },
    //查询未完成订单
    undoneList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_undone`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    //添加地址
    address(address_name) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_address(address_name) values ("${address_name}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加地址失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加地址成功。",
                        address_name
                    })
                }
            })
        })
    },
    //查询地址
    addressList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_address`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    //添加收藏
    addCollect(collect_id, collect_name, collect_img, collect_text, collect_price) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_coffee_collect(collect_id, collect_name, collect_img,collect_text,collect_price) values ("${collect_id}","${collect_name}","${collect_img}","${collect_text}","${collect_price}")`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加收藏失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "添加收藏成功。",
                        collect_id,
                        collect_name
                    })
                }
            })
        })
    },
    //查询收藏
    collectList() {
        return new Promise((resolve, reject) => {
            //编写sql语句
            let sql = `select * from db_coffee_collect`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows
                    })
                }
            })
        })
    },
    //删除收藏
    removeCollect(collect_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from db_coffee_collect where collect_id = "${collect_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除收藏失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "删除收藏成功。"
                    })
                }
            })
        })
    },
    //修改杯型
    modifyCup(con_id, con_cup) {
        return new Promise((resolve, reject) => {
            let sql = `update db_coffee_content set con_cup= "${con_cup}" where con_id= "${con_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改杯型失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "修改杯型成功。",
                        con_cup
                    })
                }
            })
        })
    },
    //修改温度
    modifyTemp(con_id, con_temp) {
        return new Promise((resolve, reject) => {
            let sql = `update db_coffee_content set con_temp= "${con_temp}" where con_id= "${con_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改温度失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "修改温度成功。",
                        con_temp
                    })
                }
            })
        })
    },
    //修改糖度
    modifySugar(con_id, con_sugar) {
        return new Promise((resolve, reject) => {
            let sql = `update db_coffee_content set con_sugar= "${con_sugar}" where con_id= "${con_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改糖度失败。",
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "修改糖度成功。",
                        con_sugar
                    })
                }
            })
        })
    }
}